package com.fieldchain.ex.fa100.ui.c2c.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.base.MyBaseFragment;
import com.fieldchain.ex.fa100.bean.C2CBean;
import com.fieldchain.ex.fa100.bean.C2CSellBean;
import com.fieldchain.ex.fa100.inter.StringCallBack;
import com.fieldchain.ex.fa100.mvp.presenter.C2CSellPresenter;
import com.fieldchain.ex.fa100.mvp.view.C2CSellView;
import com.fieldchain.ex.fa100.ui.c2c.activity.C2CSellDetailsActivity;
import com.fieldchain.ex.fa100.ui.personal.activity.LoginActivity;
import com.fieldchain.ex.fa100.utils.string.NumberFormate;
import com.fieldchain.ex.fa100.utils.string.watcher.C2CTextWatcher;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/17
 */
public class C2CSellFragment extends MyBaseFragment<C2CSellPresenter, C2CSellView> implements View.OnClickListener, C2CSellView {
    private TextView tvBalance;
    private TextView tvCompany0;
    private TextView tvPrice;
    private EditText etNum;
    private TextView tvCompany2;
    private EditText etPwd;
    private TextView tvMoneycount;
    private TextView tvSure;

    private boolean hadLogin = true;
    private C2CTextWatcher buyWatcher;
    private double price = 0;
    private String loginStr = "登录/注册";
    private String coin = "USDT";

    @Override
    protected void findView() {
        tvBalance = view.findViewById(R.id.tv_balance);
        tvCompany0 = view.findViewById(R.id.tv_company0);
        tvPrice = view.findViewById(R.id.tv_price);
        etNum = view.findViewById(R.id.et_num);
        tvCompany2 = view.findViewById(R.id.tv_company2);
        etPwd = view.findViewById(R.id.et_pwd);
        tvMoneycount = view.findViewById(R.id.tv_moneycount);
        tvSure = view.findViewById(R.id.tv_sure);
    }

    @Override
    protected void setValue() {
        needProgress = false;
        tvSure.setOnClickListener(this);

        buyWatcher = new C2CTextWatcher(etNum, new StringCallBack() {
            @Override
            public void message(String msg) {
                tvMoneycount.setText(msg);
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        presenter.unShowDialog();
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.updateUser();
        presenter.getData();

        tvCompany2.setText(coin);

    }

    @Override
    public int getLayout() {
        return R.layout.fragment_sellc2c;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_sure:
                if (!hadLogin) {
                    startActivity(new Intent(mContext, LoginActivity.class));
                } else {
                    String amount = etNum.getText().toString();
                    String password = etPwd.getText().toString();
                    presenter.recharge(amount, password);
                }
                break;
        }
    }

    public void refresh(String coin) {
        this.coin = coin;
    }

    @Override
    public void setTvSure(boolean hadLogin, String msg) {
        this.hadLogin = hadLogin;
        if (!tvSure.getText().toString().equals(msg)) {
            tvSure.setText(msg);
        }
    }

    @Override
    public void receiveData(C2CBean bean, int code) {
        if (code == 200) {
            tvCompany0.setText(bean.getCoinName().toUpperCase());
            tvCompany2.setText(bean.getCoinName().toUpperCase());
            price = bean.getSellPrice();
            buyWatcher.setPrice(price);
            tvPrice.setText(price + "");
            tvBalance.setText(NumberFormate.m(bean.getMoneyNumber(), 3));
        }
    }

    @Override
    public void requestCallBack(C2CSellBean bean, int code) {
        if (code == 200) {
            clear();
            Intent intent = new Intent(mContext, C2CSellDetailsActivity.class);
            intent.putExtra("id", bean.getInfo().getId());
            startActivity(intent);
        }
    }

    private void clear() {
        etNum.setText("");
        etPwd.setText("");
        tvMoneycount.setText("");
    }

    @Override
    public void loginShow() {
//        if(ShareManager.getInstance().getUser()==null) {
//            MyToast.show("未登录");
//        }else{
//            MyToast.show("登录过期");
//        }
    }
}
